package com.qikecn.utils;

import java.io.IOException;
import java.net.InetSocketAddress;

import com.qikecn.api.common.Contents;

import net.spy.memcached.MemcachedClient;
import net.spy.memcached.internal.OperationFuture;
import cn.geekapp.utils.LogUtil;

public class SpyMemcachedUtil {
	/**
	 * memcached客户端单例
	 */
	private static MemcachedClient cachedClient = null;
	
	//private static String memcached_server = InitPropLoader.getInstance().getProperty("memcached_server");
	
	/**
	 * 获取内存数据库实例
	 * @return
	 */
	public static MemcachedClient getInstance(){
		if(cachedClient == null){
			//初始化连接池
			// 获取连接池的实例
			/*SockIOPool pool = SockIOPool.getInstance();
			// 服务器列表及其权重
			LogUtil.showLog("memcached_server="+Contents.MEMCACHED_SERVER);
			String[] servers = { Contents.MEMCACHED_SERVER };
			Integer[] weights = { 3 };
			// 设置服务器信息
			pool.setServers(servers);
			pool.setWeights(weights);
			// 设置初始连接数、最小连接数、最大连接数、最大处理时间
			pool.setInitConn(8);
			pool.setMinConn(8);
			pool.setMaxConn(1024);
			pool.setMaxIdle(1000 * 60 * 60L);
			// 设置连接池守护线程的睡眠时间
			pool.setMaintSleep(60);
			// 设置TCP参数，连接超时
			pool.setNagle(false);
			pool.setSocketTO(60);
			pool.setSocketConnectTO(0);
			// 初始化并启动连接池
			pool.initialize();*/
			
			/*pool.setServers(servers);
		    pool.setFailover(true);
		    pool.setInitConn(10);
		    pool.setMinConn(5);
		    pool.setMaxConn(250);
		    pool.setMaintSleep(30);
		    pool.setNagle(false);
		    pool.setSocketTO(3000);
		    pool.setAliveCheck(true);
		    pool.initialize();*/
			
			// 压缩设置，超过指定大小的都压缩
			// cachedClient.setCompressEnable(true);
			// cachedClient.setCompressThreshold(1024*1024);
			
			
			
			LogUtil.showLog("memcached_server="+Contents.MEMCACHED_SERVER);
			String[] servers = { Contents.MEMCACHED_SERVER };
			String hostname = servers[0].split(":")[0];
			int port = Integer.parseInt(servers[0].split(":")[1]);
			try {
				cachedClient = new MemcachedClient(new InetSocketAddress(hostname, port));
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return cachedClient;
	}
	
	/**
	 * 只有数据不存在时进行添加的add
	 * @param key
	 * @param value
	 * @param expire 过期，单位秒
	 */
	public static OperationFuture<Boolean> add(String key, Object value, Integer expire) {
		return getInstance().add(key, expire, value);
	}
	/**
	 * 这个set的命令在memcached中的使用频率极高。set命令不但可以简单添加，如果set的key已经存在，该命令可以更新该key所对应的原来的数据，也就是实现更新的作用。
	 * @param key
	 * @param value
	 * @param expire 过期，单位秒
	 */
	public static OperationFuture<Boolean> set(String key, Object value, Integer expire) {
		return getInstance().set(key, expire, value);
	}
	/**
	 * 只有数据存在时进行替换的replace
	 * @param key
	 * @param value
	 * @param expire 过期，单位秒
	 */
	public static OperationFuture<Boolean> replace(String key, Object value, Integer expire) {
		return getInstance().replace(key, expire, value);
	}
	
	/**
	 * 删除key
	 * @param key
	 * @return
	 */
	public static OperationFuture<Boolean> delete(String key){
		return getInstance().delete(key);
	}
	
	/**
	 * 判断key是否存在
	 * @param key
	 * @return
	 */
	public static boolean keyExists(String key){
		return getInstance().get(key) != null;
	}
	
	/**
	 * 获取键值
	 * @param key
	 * @return
	 */
	public static Object get(String key) {
		return getInstance().get(key);
	}
	
	public static void main(String[] args) throws Exception {
		Contents.MEMCACHED_SERVER = "122.114.91.252:9011";
		//Contents.MEMCACHED_SERVER = "192.168.1.50:11211";
		set("test1", "111", 1);
		//Thread.sleep(900L);
		System.out.println(get("test1").toString());
	}

}
